<?php

namespace common\models\db\tables;

use Yii;

/**
 * This is the model class for table "{{%equipment_user}}".
 *
 * @property int $id
 * @property int $equipment_id 设备id
 * @property int $user_id 用户id
 * @property string $token 使用口令
 * @property string $key 认证钥匙
 * @property int $last_use_at 最后使用时间
 * @property int $status 状态
 *
 * @property EquipmentOperateLog[] $equipmentOperateLogs
 * @property Equipment $equipment
 * @property User $user
 */
class EquipmentUser extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%equipment_user}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['equipment_id', 'user_id', 'token', 'key', 'last_use_at'], 'required'],
            [['equipment_id', 'user_id', 'last_use_at', 'status'], 'integer'],
            [['token'], 'string', 'max' => 50],
            [['key'], 'string', 'max' => 32],
            [['equipment_id', 'user_id'], 'unique', 'targetAttribute' => ['equipment_id', 'user_id']],
            [['equipment_id'], 'exist', 'skipOnError' => true, 'targetClass' => Equipment::className(), 'targetAttribute' => ['equipment_id' => 'id']],
            [['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['user_id' => 'id']],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'equipment_id' => Yii::t('app', '设备id'),
            'user_id' => Yii::t('app', '用户id'),
            'token' => Yii::t('app', '使用口令'),
            'key' => Yii::t('app', '认证钥匙'),
            'last_use_at' => Yii::t('app', '最后使用时间'),
            'status' => Yii::t('app', '状态'),
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getEquipmentOperateLogs()
    {
        return $this->hasMany(EquipmentOperateLog::className(), ['for_equipment_user' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getEquipment()
    {
        return $this->hasOne(Equipment::className(), ['id' => 'equipment_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getUser()
    {
        return $this->hasOne(User::className(), ['id' => 'user_id']);
    }
}
